package com.fs.erp.api.basedata.controller;

import com.fs.erp.api.basedata.bo.product.category.ProductCategoryTreeBo;
import com.fs.erp.api.basedata.entity.ProductCategory;
import com.fs.erp.api.basedata.enums.ProductCategoryNodeType;
import com.fs.erp.api.basedata.service.ProductCategoryService;
import com.fs.erp.api.basedata.vo.product.category.CreateProductCategoryVo;
import com.fs.erp.api.basedata.vo.product.category.UpdateProductCategoryVo;
import com.fs.erp.common.utils.CollectionUtil;
import com.fs.erp.mybatis.mapper.BaseMapper;
import com.fs.erp.mybatis.service.system.IRecursionMappingService;
import com.fs.erp.web.controller.DefaultBaseController;
import com.fs.erp.web.resp.InvokeResult;
import com.fs.erp.web.resp.InvokeResultBuilder;
import com.fs.erp.web.utils.ApplicationUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.poifs.property.PropertyTableBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品类目
 */
@RestController
@RequestMapping("/basedata/product/category")
public class ProductCategoryController extends DefaultBaseController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private IRecursionMappingService recursionMappingService;

    /**
    新增类目
     */
    @ApiOperation("新增类目")
    @PostMapping()
    public InvokeResult<Void> create(@Valid CreateProductCategoryVo vo){
        productCategoryService.createCategory(vo);
        return InvokeResultBuilder.success();
    }

    /**
     * 添加子节点
     * @param id
     * @return
     */
    @GetMapping
    public InvokeResult<ProductCategoryTreeBo> createChild(@RequestParam String id){
        ProductCategory categories=productCategoryService.selectCategoryById(id);
        ProductCategoryTreeBo productCategoryTreeBo = new ProductCategoryTreeBo(categories);
        return InvokeResultBuilder.success(productCategoryTreeBo);
    }


    /**
     * 修改
     * @param vo
     * @return
     */
    @PutMapping
    public InvokeResult<Void> update(@Valid UpdateProductCategoryVo vo){
        productCategoryService.updateCategory(vo);

        productCategoryService.cleanCacheByKey(vo.getId());//根据key清除缓存

        ProductCategory category=productCategoryService.selectCategoryById(vo.getId());

        if (!vo.getAvailable()){//停用状态
            if (category.getAvailable()){
                //主节点停用，子节点全部停用
                List<String> childrenIds=recursionMappingService.getNodeChildIds(category.getId(),
                        ApplicationUtil.getBean(ProductCategoryNodeType.class));
                if (!CollectionUtil.isEmpty(childrenIds)){//子节点不为空
                    for (String childrenId:childrenIds){
                        productCategoryService.cleanCacheByKey(childrenId);
                    }
                }
            }
        }else {
            if (!category.getAvailable()){
                //子节点启用，父节点全部启用
                List<String> parentIds=recursionMappingService.getNodeParentIds(category.getParentId(),
                        ApplicationUtil.getBean(ProductCategoryNodeType.class));
                if (!CollectionUtil.isEmpty(parentIds)){
                    for (String parentId:parentIds){
                        productCategoryService.cleanCacheByKey(parentId);
                    }
                }
            }
        }
        return InvokeResultBuilder.success();
    }

    /**
     * 查询
     */
    @GetMapping("/query")
    public InvokeResult<List<ProductCategoryTreeBo>> queryCategory(){
        List<ProductCategory> categories=productCategoryService.getAllProductCategory();
        if (CollectionUtil.isEmpty(categories)){
            return InvokeResultBuilder.success(CollectionUtil.emptyList());
        }
        List<ProductCategoryTreeBo> categoryTrees=categories.stream().map(ProductCategoryTreeBo::new).collect(Collectors.toList());
        return InvokeResultBuilder.success(categoryTrees);
    }

    /**
     * 批量停用
     * @return
     */
    @PatchMapping("/unable/batch")
    public InvokeResult<Void> batchUnable(@ApiParam(value = "ID",required = true)
           @RequestBody List<String> ids){
        productCategoryService.batchUnable(ids);
        return InvokeResultBuilder.success();
    }

    /**
     * 批量使用
     * @param ids
     * @return
     */
    @PatchMapping("/enable/batch")
    public InvokeResult<Void> batchEnable(@ApiParam(value = "ID",required = true)
           @RequestBody List<String> ids){
        productCategoryService.batchEnable(ids);
        return InvokeResultBuilder.success();
    }
}
